package com.example.apple.printingroommanagementsystem;

import android.app.Application;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * Created by apple on 17/2/24.
 */

public class GetRequest {

    public void doGet(String ip,String s,final Handler handler) {
        //拼接url地址
        final String getUrl =ip +s;
        //开启一个线程
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    //创建url
                    URL url =new URL(getUrl);
                    //生成HttpURLConnection对象
                    HttpURLConnection httpUrlConnection = (HttpURLConnection) url.openConnection();
                    //连接服务器
                    httpUrlConnection.connect();
                    //用反应码看服务器是否响应
                    if (httpUrlConnection.getResponseCode()==200){
                        //  Toast.makeText(getApplicationContext(), "访问成功", Toast.LENGTH_SHORT).show();
                        //接受输入流
                        InputStream inputStream = httpUrlConnection.getInputStream();
                        //处理接收到的数据，一行一行读取
                        BufferedReader buffereReader = new BufferedReader(new InputStreamReader(inputStream));
                        //读取的每一行用StringBuffer去装载
                        StringBuffer stringBuffer = new StringBuffer();

                        String readLine = "";
                        //循环得到的stringBuffer是json字符串
                        while ((readLine = buffereReader.readLine())!=null){
                            stringBuffer.append(readLine);
                        }
                        Log.e("|-->",stringBuffer.toString());
                        //关闭底层输入流
                        inputStream.close();
                        //关闭上层读取流
                        buffereReader.close();
                        //关闭网络连接
                        httpUrlConnection.disconnect();
                        //把json字符串转换成json对象
                        JSONObject jsonObject =new JSONObject(stringBuffer.toString());
                        //返回接受到的数据
                        Message mag = new Message();
                        mag.obj = jsonObject;
                        handler.sendMessage(mag);

                    }else {
                        // Toast.makeText(getApplicationContext(), "访问失败", Toast.LENGTH_SHORT).show();
                    }
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        }).start();

    }
    public static File download(String path){
        HttpURLConnection conn = null;
        File file = null;
        try {
            URL url = new URL(path);
            conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            conn.setConnectTimeout(5000);
            conn.setReadTimeout(50000);
            conn.connect();
            int code = conn.getResponseCode();

            if(code == 200){
                InputStream is = conn.getInputStream();
                byte[] b = new byte[1024];
                int len = 0;
                String filenpath = Environment.getExternalStorageDirectory().getAbsolutePath()+"/wenyin2.apk";
                file = new File(filenpath);

                if(file.exists()){
                    file.delete();
                }

                FileOutputStream fos = new FileOutputStream(file);

                while((len = is.read(b)) != -1){
                    fos.write(b,0,len);
                }
                fos.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally{
            conn.disconnect();
        }
        return file;
    }
}
